const vm = new Vue({
    el: '#main',
    data: {
        chatList: [],
    },
    methods: {
        chatting,
    },
});

async function main() {
    mui.init();
    fxj.fixLayout();
    getChatList();
    setInterval(() => {
        getChatList();
    }, 2000);
}

function apiready() {
    main().catch(
        e => {
            console.error(e);
        },
    );
    setCustomRefreshHeaderInfo(() => {
        getChatList();
    });
}

function getChatList() {
    ajax({
        url: 'home/chat/getList',
    }).then(
        res => {
            let tempArr = [];
            if (res.data !== null) {
                tempArr = Object.values(res.data).map(chat => {
                    chat.date = new Date(Number(chat.date.toString() + '000')).toLocaleString();
                    return chat;
                });
            }
            vm.chatList = tempArr;
        },
    );
}

function delChat(id) {
    ((cb) => {
        fxj.dialog.loading({cb});
    })(
        (c) => {
            ajax({
                url: 'home/chat/delete',
                data: {
                    otherId: id,
                },
            }).then(
                res => {
                    c();
                    getChatList();
                },
            );
        },
    );
}

function chatting(evt, id) {
    const target = evt.target;
    if (target.nodeName === 'A') {
        delChat(id);
    }
    else {
        goPage('chat.html', {
            data: {
                landlordId: id,
            },
        });
    }

}
